package com.dashu.core.controller;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.core.dto.*;
import com.dashu.core.model.Company;
import com.dashu.core.model.TeacherCls;
import com.dashu.core.result.PageEnty;
import com.dashu.core.result.ResultBean;
import com.dashu.core.service.CompanyService;
import com.dashu.core.service.RelationshipService;
import com.dashu.core.service.TeacherClsService;
import com.dashu.core.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author xys
 * @Date 2020/ReplRyCode.SUCCESS.getCode()ReplRyCode.SUCCESS.getCode()/ReplRyCode.SUCCESS.getCode()7 ReplRyCode.SUCCESS.getCode()5:2ReplRyCode.SUCCESS.getCode()
 */

@RequestMapping("dashu/relation")
@RestController
@Slf4j
@Api(value = "dashu/relation", tags = {"关联模块"}, description = "后台")
public class RelationController {

    @Autowired
    private RelationshipService relationshipService;
    @Autowired
    private TeacherClsService teacherClsService;
    @Autowired
    private CompanyService companyService;
//    @Autowired
//    RestTemplate restTemplate;

    @PostMapping("/addParent")
    @ApiOperation(value = "家长关联添加", notes = "家长学生关联添加")
    public ResultBean<RelationshipDTO> addDepartment(@RequestBody @Validated RelationshipVO relationshipVO) {
        if (relationshipVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "家长学生关联添加", relationshipService.add(relationshipVO));
    }


    @DeleteMapping("/deleteParent")
    @ApiOperation(value = "家长关联信息删除", notes = "家长学生关联信息删除")
    public ResultBean<Boolean> deleteParent(long relationshipId) {
        if (relationshipId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "家长学生关联信息删除", relationshipService.deleteByRelationshipId(relationshipId));
    }

    @PostMapping("/pageParentList")
    @ApiOperation(value = "家长关联信息列表", notes = "家长学生关联信息列表")
    public ResultBean<PageDTO<RelationshipListDTO>> pageParentList(@RequestBody @Validated PageEnty<RelationshipListVO> relationshipListVO) throws Exception {
        PageDTO<RelationshipListDTO> pageDTO = new PageDTO<>();
        if (relationshipListVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = relationshipService.relationshipCount(relationshipListVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(relationshipService.pageRelationshipList(relationshipListVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "家长学生关联信息列表", pageDTO);
    }

    @PostMapping("/addTeacher")
    @ApiOperation(value = "教师关联添加", notes = "教师班级关联添加")
    public ResultBean<TeacherClsDTO> addTeacher(@RequestBody @Validated TeacherClsVO teacherClsVO) {
        if (teacherClsVO == null)
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        TeacherCls teacherCls = teacherClsService.existTeacherClsIden(teacherClsVO.getBindCls(), teacherClsVO.getIdentity());
        if (teacherCls != null)
            return new ResultBean<>(ReplRyCode.EXIST_ClS_TEACHER_IDEA.getCode(), ReplRyCode.EXIST_ClS_TEACHER_IDEA.getMessage(), null);

        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师关联添加", teacherClsService.add(teacherClsVO));
    }

    @DeleteMapping("/deleteTeache")
    @ApiOperation(value = "教师关联信息删除", notes = "教师班级关联信息删除")
    public ResultBean<Boolean> deleteTeache(long TeacheClsId) {
        if (TeacheClsId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师班级关联信息删除", teacherClsService.deleteByTeacherClsId(TeacheClsId));
    }


    @PostMapping("/updataRule")
    @ApiOperation(value = "教师关联信息修改", notes = "教师班级关联信息修改")
    public ResultBean<TeacherClsDTO> updataRule(@RequestBody @Validated UpdateEntityVO<UpTeacherClsVO> updateEntityVO) throws Exception {
        if (updateEntityVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        TeacherClsDTO teacherClsDTO = teacherClsService.updataByTeacherClsId(updateEntityVO);
        if (teacherClsDTO != null) {
            return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师班级关联信息修改", teacherClsDTO);
        }
        return new ResultBean<>(ReplRyCode.PERMISSION_NOT_DATA.getCode(), ReplRyCode.PERMISSION_NOT_DATA.getMessage(), teacherClsDTO);
    }


    @PostMapping("/pageTeacherList")
    @ApiOperation(value = "教师关联信息列表", notes = "教师班级关联信息列表")
    public ResultBean<List<TeacherClsListDTO>> pageTeacherList(@RequestBody @Validated PageEnty<TeacherClsListVO> teacherClsPageEntyVO) throws Exception {
        if (teacherClsPageEntyVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "教师班级关联信息列表", teacherClsService.pageTeacherList(teacherClsPageEntyVO));
    }

    @DeleteMapping("/deleteCompany")
    @ApiOperation(value = "学校关联信息删除", notes = "学校关联信息删除")
    public ResultBean<Boolean> deleteCompany(long companyId) throws Exception {
        if (companyId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "学校关联信息删除", companyService.deleteRelation(companyId));
    }

    @PostMapping("/relationCompany")
    @ApiOperation(value = "学校关联信息", notes = "学校关联信息")
    public ResultBean<Boolean> relationCompany(@RequestBody @Validated RelationshipCompanyVO relationshipCompanyVO) throws Exception {
        if (relationshipCompanyVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "学校关联信息", companyService.creteRelation(relationshipCompanyVO));
    }


}
